package com.imooc.controller;

import com.imooc.common.constants.ResultCodeConstants;
import com.imooc.common.result.ResponseData;
import com.imooc.common.result.ResponseUtils;
import com.imooc.dto.category.CategoryListResponse;
import com.imooc.dto.index.CarouselListResponse;
import com.imooc.pojo.Category;
import com.imooc.pojo.vo.CategoryVO;
import com.imooc.service.ICarouselService;
import com.imooc.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: IndexController
 * @Description: 首页
 * @author: limingxing
 * @Date: 2019-12-24 16:00
 */
@Api(value = "首页", tags = "首页展示的相关接口")
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private ICarouselService carouselService;

    @ApiOperation(value = "获取首页轮播图列表", notes = "获取首页轮播图列表", httpMethod = "GET")
    @GetMapping("/carousel")
    public ResponseData<CarouselListResponse> carousel() {
        CarouselListResponse response = carouselService.queryAll(1);
        if(response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())){
            return ResponseUtils.success(response.getCarouselDtoList());
        }
        return ResponseUtils.success(response.getMsg());

    }

    /**
     * 首页分类展示需求：
     * 1. 第一次刷新主页查询大分类，渲染展示到首页
     * 2. 如果鼠标上移到大分类，则加载其子分类的内容，如果已经存在子分类，则不需要加载（懒加载）
     */
    @ApiOperation(value = "获取商品分类(一级分类)", notes = "获取商品分类(一级分类)", httpMethod = "GET")
    @GetMapping("/cats")
    public ResponseData<CategoryListResponse> cats() {
        CategoryListResponse response = categoryService.queryAllRootLevel();
        if(response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())){
            return ResponseUtils.success(response.getList());
        }
        return ResponseUtils.success(response.getMsg());
    }


    @ApiOperation(value = "获取商品子分类", notes = "获取商品子分类", httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public ResponseData subCat(
            @ApiParam(name = "rootCatId", value = "一级分类id", required = true)
            @PathVariable Integer rootCatId) {

        if (rootCatId == null) {
            return ResponseUtils.errorMsg("分类不存在");
        }

        List<CategoryVO> list = categoryService.getSubCatList(rootCatId);
        return ResponseUtils.success(list);
    }


}
